﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL;
using DTO;
using System.Data;
using System.Text;
using System.Reflection;

namespace GUI.Common
{
    public class ExportToExcel
    {
        public void TaoBangGia(string fileName)
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "application/csv";
            context.Response.Charset = "";
            context.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
            context.Response.ContentEncoding = Encoding.Unicode;
            context.Response.BinaryWrite(Encoding.Unicode.GetPreamble());
            SanPhamBLL spBLL = new SanPhamBLL();
            List<SanPhamDTO> lst = new List<SanPhamDTO>();
            lst = spBLL.GetListSanPhamToBangGia();

            DataTable dtb = ListToDataTable(lst);

            try
            {
                StringBuilder sb = new StringBuilder();
                //Add Header
                int[] icolumn = new int[] { 0, 3, 6, 13 };
                for (int count = 0; count < icolumn.Length; count++)
                {
                    if (dtb.Columns[icolumn[count]].ColumnName != null)
                        sb.Append(dtb.Columns[icolumn[count]].ColumnName);
                    if (count < icolumn.Length - 1)
                    {
                        sb.Append("\t");
                    }
                }
                context.Response.Write(sb.ToString() + "\n");
                context.Response.Flush();
                //Append Data
                int soDem = 0;
                while (dtb.Rows.Count >= soDem + 1)
                {
                    sb = new StringBuilder();

                    for (int col = 0; col < icolumn.Length - 1; col++)
                    {
                        if (dtb.Rows[soDem][icolumn[col]] != null)
                            sb.Append(dtb.Rows[soDem][icolumn[col]].ToString().Replace(",", " "));
                        sb.Append("\t");
                    }
                    if (dtb.Rows[soDem][dtb.Columns.Count - 1] != null)
                        sb.Append(dtb.Rows[soDem][dtb.Columns.Count - 1].ToString().Replace(",", " "));

                    context.Response.Write(sb.ToString() + "\n");
                    context.Response.Flush();
                    soDem = soDem + 1;
                }

            }
            catch (Exception ex)
            {
                context.Response.Write(ex.Message);
            }
            dtb.Dispose();
            context.Response.End();
        }

        public static DataTable ListToDataTable<T>(List<T> list)
        {
            DataTable dt = new DataTable();

            foreach (PropertyInfo info in typeof(T).GetProperties())
            {
                dt.Columns.Add(new DataColumn(info.Name, info.PropertyType));
            }
            foreach (T t in list)
            {
                DataRow row = dt.NewRow();
                foreach (PropertyInfo info in typeof(T).GetProperties())
                {
                    row[info.Name] = info.GetValue(t, null);
                }
                dt.Rows.Add(row);
            }
            return dt;
        }
    }
}